//
//  UserModel.m
//  SkyworthPalmExam
//
//  Created by Arvin Wang on 15/1/23.
//  Copyright (c) 2015年 Arvin Wang. All rights reserved.
//

#import "UserModel.h"


@implementation UserModel

-(BOOL)deleteUser
{
    BOOL deleteSucc = [[Singleton shareInstance].userDB executeUpdate:@"DELETE FROM user"];
    return deleteSucc;
}

- (BOOL)save
{
    // 删除security
    BOOL deleteSucc = [[Singleton shareInstance].userDB executeUpdate:@"DELETE FROM user"];
    if (!deleteSucc) {
        NSLog(@"删除失败");
    }
    
    // 保存security
    BOOL insertSucc = [[Singleton shareInstance].userDB executeUpdate:@"INSERT INTO user (id,isNewRecord,remarks,createDate,updateDate,loginName,userName,userPassword,userType,loginFlag,isCertification,isInit,areaName) VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
                      self.id,self.isNewRecord,self.remarks,self.createDate,self.updateDate,self.loginName,self.userName,self.userPassword,self.userType,self.loginFlag,self.isCertification,self.isInit,self.areaName];
    if (!insertSucc) {
        NSLog(@"添加失败\n%@",  [[Singleton shareInstance].userDB lastError]);
    }

    return insertSucc;
}


+ (id)getUser
{
    UserModel *model = nil;
    NSString *sql = @"SELECT * FROM user LIMIT 1";
    FMResultSet *rs = [[Singleton shareInstance].userDB executeQuery:sql];
    if ([rs next]) {
        NSDictionary *dic = [rs resultDictionary];
        model = [[UserModel alloc] initWithDictionary:dic error:nil];
    }
    return model;
}

- (void)updateUserCertification
{
    BOOL updateCer = [[Singleton shareInstance].userDB executeUpdate:@"UPDATE user SET isCertification = '1'"];
    if (!updateCer) {
        NSLog(@"更新失败");
    }
}

- (void)updateInventoryInit
{
    BOOL uadateInit = [[Singleton shareInstance].userDB executeUpdate:@"UPDATE user SET isInit = '1'"];
    if (!uadateInit) {
        NSLog(@"更新失败");
    }
}

- (void)setUserName:(NSString<Optional> *)userName {
    _userName = userName;
    [NSUserDefaults saveObject:userName forKey:@"realName"];
}

@end
